// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        background:cc.Node,
        zawLayer:cc.Node,
    },


    onLoad () {
        //设置背景图高度和屏幕高度一样
        this.background.height = cc.view.getVisibleSize().height;
        //开启物理引擎
        cc.director.getPhysicsManager().enabled = true;
        //初始化游戏数据
        this.initGame();
    },
    initGame()
    {
        this.isGaming = true;
        this.gameCount = 0;
        this.zawModel = require('zawModel');
         /*预加载资源*/
         var self = this;
         cc.loader.loadRes("/game/game_bstart", cc.SpriteFrame, function (err, spriteFrame) {  
            self.xingSprite = spriteFrame; 
         }); 
        cc.loader.loadRes("/game/game_xian", cc.SpriteFrame, function (err, spriteFrame) {  
            self.xianSprite = spriteFrame;
        }); 

    },

    start ()
    {
         // this.zawModel.startBomb(this,function(){
        //     console.log('lllllll');
        // });

        // this.zawModel.startFeiBiao(this,function(){
        //     console.log("wwwww");
        // })

        // this.zawModel.startPlane(this,function(){
        //     console.log("plane over");
        // })
        
        // this.zawModel.startStaticBall(this,function(){
        //     console.log("qqqq");
        // })

        // this.zawModel.startDynamicBall(this,function(){
        //     console.log("qqqq");
        // })

        this.zawModel.startTuiXing(this,function(){
            console.log("qqqq");
        })
        
    },

    update (dt) 
    {
        if(!this.isGaming) return;
        ++this.gameCount;
        
        var w  = cc.view.getVisibleSize().width/2;
        if(this.gameCount%75==0 && this.xingSprite)
        {//每隔2秒执行一次
            var star = new cc.Node('gameStart');
            const sprite = star.addComponent(cc.Sprite);
            sprite.spriteFrame = this.xingSprite; 
            star.x = this.getRandomNumber(-w,w);
            star.y = 600;
            var s = this.getRandomNumber(5,15)/10;
            star.setScale(s,s);
            star.opacity = this.getRandomNumber(100,200);
            this.background.addChild(star);
            var seq = cc.sequence(cc.moveTo(15,cc.v2(star.x,-700)), cc.removeSelf())
            star.runAction(seq);
        }

        if(this.gameCount%75==0  && this.xianSprite){//隔了1.5秒执行一次
            var star = new cc.Node('gameStart_line');
            const sprite = star.addComponent(cc.Sprite);
            sprite.spriteFrame = this.xianSprite; 
            star.x = this.getRandomNumber(-w,w);
            star.y = 800;
            star.setScale(0.8,1);
            this.background.addChild(star);
            var seq = cc.sequence(cc.moveTo(3,cc.v2(star.x,-800)),cc.removeSelf())
            star.runAction(seq);
        }

        if(this.gameCount%300==0){//隔了3秒执行一次
            
        }

        if(this.gameCount==9000){this.gameCount = 0}//计时器归零处理
    },
    /*获取从a~b之间的一个随机数*/
    getRandomNumber:function(a,b){
        return Math.floor(Math.random()*10000)%(Math.abs(b-a))+a;
     },
});
